package com.ilop.siteraudio.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.base.impl.DialogClickCallBack;

/**
 * @author skygge
 * @date 2019-10-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：基础弹出框
 */
public class BaseDialog extends Dialog {

    TextView tvDialogTip;
    TextView tvDialogCancel;
    TextView tvDialogConfirm;
    View mDialogLineView;

    private String mMsg;
    private String mCancel;
    private String mConfirm;
    private boolean isGone = false;

    private DialogClickCallBack mDialogCallBack;

    public BaseDialog(Context context) {
        super(context, R.style.dialog_background);
    }

    public BaseDialog(Context context, DialogClickCallBack callBack) {
        super(context, R.style.dialog_background);
        this.mDialogCallBack = callBack;
    }

    public void setCancelVisibility(boolean isVisibility){
        this.isGone = isVisibility;
    }

    public void setCancelText(String cancel){
        this.mCancel = cancel;
    }

    public void setTitleAndButton(String msg, String cancel, String confirm) {
        this.mMsg = msg;
        this.mConfirm = confirm;
        this.mCancel = cancel;
    }

    public void setMsg(String msg) {
        this.mMsg = msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog);
        setCanceledOnTouchOutside(false);

        tvDialogTip = findViewById(R.id.tv_dialog_tip);
        mDialogLineView = findViewById(R.id.iv_dialog_line);
        tvDialogCancel = findViewById(R.id.tv_dialog_cancel);
        tvDialogConfirm = findViewById(R.id.tv_dialog_confirm);

        tvDialogConfirm.setVisibility(isGone ? View.GONE : View.VISIBLE);
        mDialogLineView.setVisibility(isGone ? View.GONE : View.VISIBLE);

        if (!TextUtils.isEmpty(mMsg)) tvDialogTip.setText(mMsg);
        if (!TextUtils.isEmpty(mCancel)) tvDialogCancel.setText(mCancel);
        if (!TextUtils.isEmpty(mConfirm)) tvDialogConfirm.setText(mConfirm);

        tvDialogConfirm.setOnClickListener(v -> {
            if (mDialogCallBack != null) mDialogCallBack.onConfirm();
            dismiss();
        });

        tvDialogCancel.setOnClickListener(v -> {
            if (mDialogCallBack!=null) mDialogCallBack.onCancel();
            dismiss();
        });
    }

    @Override
    public void onBackPressed() {
        if (mDialogCallBack!=null) mDialogCallBack.onCancel();
        super.onBackPressed();
    }
}
